//
//  FeedContentView.m
//  兼职页面
//
//  Created by kuiyuan liang on 2021/5/26.
//

#import "FeedContentView.h"
#import "CommonUtil.h"
#import <SDWebImage/SDWebImage.h>
#import <GKPhotoBrowser/GKPhotoBrowser.h>

@interface FeedContentView()

@property (strong, nonatomic) UILabel * contentLabel;



@property (strong, nonatomic) NSMutableArray * imageViews;

@end

@implementation FeedContentView

-(instancetype)initWithFrame:(CGRect)frame{
    if (self = [super initWithFrame:frame]) {
        [self addSubview:self.contentLabel];
        [self addSubview:self.replayBtn];
        [self addSubview:self.reportBtn];
        
        for (NSInteger i = 0; i < 9; i++) {
            UIImageView *imageV = [UIImageView new];
            imageV.userInteractionEnabled = YES;
            UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(imageViewClick:)];
            [imageV addGestureRecognizer:tap];
            [self.imageViews addObject:imageV];
        }
    }
    return self;
}

-(void)setModel:(MessageModel *)model{
    _model = model;
    
    for (UIImageView *imageV in self.imageViews) {
        [imageV removeFromSuperview];
    }
    // 文字区域
    self.contentLabel.text = model.content;
    CGFloat height = [CommonUtil countHeight:model.content font:self.contentLabel.font width:self.contentLabel.width];
    self.contentLabel.height = height;

    
    // 图片区域
    CGFloat lastBottom = _contentLabel.bottom;
    CGFloat imageWH = model.imageWH;
    CGFloat lastImageViewBottom = lastBottom;
    for (NSInteger i = 0; i < model.imageCount; i++) {
        UIImageView *imageV = self.imageViews[i];
        CGFloat x = AD(70) + ((i%3) * (imageWH + AD(10)));
        CGFloat y = lastBottom + AD(10) + ((i/3) * (imageWH + AD(10)));
        imageV.frame = CGRectMake(x, y, imageWH, imageWH);
        [self addSubview:imageV];
        lastImageViewBottom = imageV.bottom;
        imageV.tag = i;
        [imageV sd_setImageWithURL:[NSURL URLWithString:TestImageURL]];
    }
    lastBottom = lastImageViewBottom;
    
    self.replayBtn.top = lastBottom  + AD(7);
    self.reportBtn.top = self.replayBtn.top;

}


- (void)imageViewClick:(UITapGestureRecognizer *)tap{
    NSLog(@"%zd",tap.view.tag);
    NSMutableArray *photos = [NSMutableArray array];
    for (NSInteger i = 0; i < self.model.imageCount; i++) {
        GKPhoto *photo = [GKPhoto new];
        photo.url = [NSURL URLWithString:TestImageURL];
        [photos addObject:photo];
    }

    GKPhotoBrowser *browser = [GKPhotoBrowser photoBrowserWithPhotos:photos currentIndex:tap.view.tag];
    browser.showStyle = GKPhotoBrowserShowStyleNone;
    [browser showFromVC:self.superVC];
}



-(UILabel *)contentLabel{
    if (!_contentLabel) {
        _contentLabel = [[UILabel alloc] initWithFrame:CGRectMake(AD(70),0, SCREEN_WIDTH-AD(140), AD(25))];
        _contentLabel.font = [UIFont systemFontOfSize:AD(13)];
        _contentLabel.textColor = [UIColor blackColor];
        _contentLabel.numberOfLines = 0;
    }
    return _contentLabel;
}

-(UIButton *)replayBtn{
    if (!_replayBtn) {
        _replayBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_replayBtn setTitle:@"回复" forState:UIControlStateNormal];
        [_replayBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        _replayBtn.frame = CGRectMake(_contentLabel.left, 0, AD(45), AD(20));
        _replayBtn.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.1];
        _replayBtn.layer.cornerRadius = AD(10);
        _replayBtn.clipsToBounds = YES;
        _replayBtn.titleLabel.font = [UIFont systemFontOfSize:AD(11)];
    }
    return _replayBtn;
}

-(UIButton *)reportBtn{
    if (!_reportBtn) {
        _reportBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        _reportBtn.frame = CGRectMake(SCREEN_WIDTH-AD(100), 0, AD(80), AD(20));
        _reportBtn.titleLabel.font = [UIFont systemFontOfSize:AD(14)];
        [_reportBtn setTitle:@"举报" forState:UIControlStateNormal];
        [_reportBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [_reportBtn setImage:[UIImage imageNamed:@"icon_warning"] forState:UIControlStateNormal];
        _reportBtn.titleEdgeInsets = UIEdgeInsetsMake(0, 0, 0, -AD(10));
    }
    return _reportBtn;
}

-(NSMutableArray *)imageViews{
    if (!_imageViews) {
        _imageViews = [NSMutableArray array];
    }
    return _imageViews;
}

@end
